package com.qin.common;

import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

@Component
public class Shuffle {

    /*
     * 入参：1, 5, 2, 4
     * 排序前：{1=1, 2=2, 3=3, 4=4, 5=5}
     * 排序后：{1=1, 2=4, 3=2, 4=3, 5=5}
     *
     * 参数说明：
     * start_order：开始下标，end_order：结束下标
     * old_order：移动前下标，new_order：移动后下标
     */
    public static Map<Integer, Integer> orderShuffle(int start_order, int end_order, int old_order, int new_order){

        //初始化map
        Map<Integer, Integer> map = new HashMap();
        for(int i=start_order; i<=end_order; i++){
            map.put(i, i);
        }
        System.out.println("排序前：" + map.toString());

        map.put(old_order, new_order);
        if(new_order > old_order){
            for(int i=old_order+1; i<=new_order; i++){
                map.put(i, i-1);
            }
        }
        else if(new_order < old_order){
            for(int i=old_order-1; i>=new_order; i--){
                map.put(i, i+1);
            }
        }

        System.out.println("排序后：" + map.toString());

        return map;
    }
}
